package org.box.metadata.shell;

import java.io.ByteArrayOutputStream;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.box.metadata.shell.web.NativeServer;
import org.box.metadata.shell.web.ws.cs.ServerLogService;
import org.eclipse.jetty.server.Server;

import com.google.inject.AbstractModule;
import com.google.inject.Scopes;
import com.google.inject.matcher.Matchers;

public class WebConsoleModule extends AbstractModule {

	public static class ServerOutStream extends ByteArrayOutputStream {

		public ServerOutStream(int bufferSize) {
			super(bufferSize);
		}
	};
	
	private final WebConsoleDefaultConfiguration cfg;

	public WebConsoleModule(WebConsoleDefaultConfiguration cfg) {
		this.cfg = cfg;
	}

	@Override
	protected void configure() {
		bind(WebConsoleDefaultConfiguration.class).toInstance(cfg);
		bind(Server.class).to(NativeServer.class).in(Scopes.SINGLETON);
		bind(ServerOutStream.class).toInstance(new ServerOutStream(cfg.getBufferSize()));
		bind(ServerLogService.class);
		
		bindInterceptor(Matchers.subclassesOf(ServerLogService.class), 
						Matchers.any(), 
						new MethodInterceptor() {

			@Override
			public Object invoke(MethodInvocation invocation) throws Throwable {
				System.out.println(invocation.getMethod()); 
				return invocation.proceed();
			}}
		);
	}

}
